package com.ttwm.controller;


import com.ttwm.pojo.Order;
import com.ttwm.pojo.OrderFood;
import com.ttwm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

//    添加订单
    @PostMapping(value = {"/ttwm/addOrder"})
    public Map<String ,Object> insertOrder(@RequestBody Order order){
        Map map = new HashMap<String ,Object>();
        int insert = orderService.insertOrder(order);
        if (insert>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

//  查询所以订单
    @PostMapping(value = {"/ttwm/allOrder"})
    public Map<String ,Object> allOrder(){
        Map map = new HashMap<String ,Object>();
        List<Order> orderList = orderService.allOrder();
        if (orderList.isEmpty()){
            map.put("success",false);
            map.put("msg","失败");
        }else {
            map.put("success",true);
            map.put("msg","成功");
            map.put("list",orderList);
        }
        return map;
    }
//  订单查询订单详情
    @PostMapping(value = {"/ttwm/allOrderToFood"})
    public Map<String ,Object> allOrderToFood(@RequestBody Order order){
        Map map = new HashMap<String ,Object>();
        List<Order> allOrderToFood = orderService.allOrderToFood(order);
        if (allOrderToFood.isEmpty()){
            map.put("success",false);
            map.put("msg","失败");
        }else {
            map.put("success",true);
            map.put("msg","成功");
            map.put("list",allOrderToFood);
        }
        return map;
    }

    //查询一个订单
    @PostMapping(value = {"/ttwm/oneOrderToFood"})
    public Map<String ,Object> oneOrderToFood(@RequestBody Order order){
        Map map = new HashMap<String ,Object>();
        Order oneOrderToFood = orderService.oneOrderToFood(order);
        if (oneOrderToFood != null){
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",oneOrderToFood);
        }else {
           map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    // 修改为已支付
    @PostMapping(value = {"/ttwm/updatePayment"})
    public Map<String ,Object> updatePayment(@RequestBody Order order){
        Map map = new HashMap<String ,Object>();
        int update = orderService.updatePayment(order);
        if (update>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

    // 删除订单
    @PostMapping(value = {"/ttwm/deleteOrder"})
    public Map<String ,Object> deleteOrder(@RequestBody Order order){
        Map map = new HashMap<String ,Object>();
        int deleteOrder = orderService.deleteOrder(order);
        if (deleteOrder>0){
            map.put("success",true);
            map.put("msg","成功");
        }else {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }

//    修改地址
@PostMapping(value = {"/ttwm/updateLocation"})
public Map<String ,Object> updateLocation(@RequestBody Order order){
    Map map = new HashMap<String ,Object>();
    int updateLocation = orderService.updateLocation(order);
    if (updateLocation>0){
        map.put("success",true);
        map.put("msg","成功");
    }else {
        map.put("success",false);
        map.put("msg","失败");
    }
    return map;
}
}
